package com.begye.smartloan.apptest

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.os.Build
import coil.Coil
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import com.begye.smartloan.apptest.tool.SpTool
import com.begye.smartloan.apptest.tool.ToastTool
import com.data.analysislib.AnalysisBuilder
import com.data.analysislib.AnalyticsUtil
import com.data.datalib.ASSycBuilder
import com.data.datalib.ASSycManager
import com.util.utillib.extend.appVersionCode

class MyApplication : Application() {

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context

        @SuppressLint("StaticFieldLeak")
        lateinit var instance: MyApplication

        var appVersion: Int = 0
        const val product = "BEGYESIKA"

        const val storeLink =
            "https://play.google.com/store/apps/details?id=com.begye.smartloan.app"

        const val policyLink = "https://sites.google.com/bayportservice.com/bearcreditservice"

        var pCode: String = ""

        var pServer: String = ""
    }

    override fun onCreate() {
        super.onCreate()
        context = applicationContext
        instance = this
        appVersion = appVersionCode()

        initGifLib()

        SpTool.init(context = this)
        ToastTool.init(context = this)

        ASSycManager.init(this)

        AnalyticsUtil.init(
            context = this,
            gpReferrerCallback = {

            },
            adIdCallback = {
                ASSycBuilder.androidId = AnalyticsUtil.getAndroidId()
                ASSycBuilder.advertId = AnalyticsUtil.getAdId()
            })
    }

    private fun initGifLib() {
        val imageLoader = ImageLoader.Builder(this)
            .componentRegistry {
                if (Build.VERSION.SDK_INT >= 28) {
                    add(ImageDecoderDecoder(this@MyApplication))
                } else {
                    add(GifDecoder())
                }
            }.build()
        Coil.setImageLoader(imageLoader)
    }

    fun initDataLibs(server: String, code: String) {
        pCode = code
        pServer = server

        AnalysisBuilder.zone = server
        ASSycBuilder.zone = server

        AnalyticsUtil.init(
            context = this,
            gpReferrerCallback = {

            },
            adIdCallback = {
                ASSycBuilder.androidId = AnalyticsUtil.getAndroidId()
                ASSycBuilder.advertId = AnalyticsUtil.getAdId()
            })
    }

    fun initLibs(tk: String, uid: String) {
        ASSycBuilder.token = tk
        AnalysisBuilder.token = tk
        AnalysisBuilder.userId = uid

        AnalyticsUtil.init(
            context = this,
            gpReferrerCallback = {

            },
            adIdCallback = {
                ASSycBuilder.androidId = AnalyticsUtil.getAndroidId()
                ASSycBuilder.advertId = AnalyticsUtil.getAdId()
            })
    }

    fun isTargetServerFr(): Boolean {
        val keywords = listOf("fcfa", "xaf", "senegal")
        return keywords.any { pServer.endsWith(it, ignoreCase = true) }
    }
}